package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modelo.LimitesEvento;

import controle.ArduinoIntervalController;
import controleBD.UsuarioEventoDAO;

@WebServlet("/RemoveEventoServlet")
public class RemoveEventoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public RemoveEventoServlet() {
        super();
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	if(request.getParameter("button").equals("Remover")){

    	
    		if(request.getParameter("check") != null){
	    		
    			UsuarioEventoDAO eventoDAO = new UsuarioEventoDAO();
	    		
	    		String usuario = request.getSession().getAttribute("username").toString();
	    		
	    		ArrayList<LimitesEvento> lista = eventoDAO.retornaEventos(usuario);
	    		
	    		String evento = lista.get(Integer.parseInt(request.getParameter("check"))).getEvento();
	    		String min = lista.get(Integer.parseInt(request.getParameter("check"))).getMin();
	    		String max = lista.get(Integer.parseInt(request.getParameter("check"))).getMax();
	    		
	    		
	    		eventoDAO.removeEvento(usuario, evento, min, max);
	    		
	    		ArduinoIntervalController.reduzValoresLuz(Integer.parseInt(min), Integer.parseInt(max));
    		}
    		}
    	
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		out.print("<body onLoad=\"window.close()\"> </body>");
    	
    		
	}

}
